#include <sgu-0/sgu.h>
#include "test.h"

using namespace SGU;

static std::string escape(const char * s)
{
	std::string a;
	while(*s)
	{
		switch(*s)
		{
		case '\\' : a.append("\\\\"); break;
		case '\'' : a.append("\\\'"); break;
		case '\"' : a.append("\\\""); break;
		case '\n' : a.append("\\n"); break;
		case '\r' : a.append("\\r"); break;
		
		default:
			if(*s<32)
			{
				char tmp[] = "\\xHH";
				ASSERT(sizeof(tmp)==5);
				utobase((unsigned char)*s,tmp+2,16,2);
				a += tmp;
			}
			else
				a += *s;
		}
		++s;
	}
	return a;
}


std::string Test::nocolon(const std::string & str)
{
	size_t z = str.size();
	std::string a;
	LOOP(size_t,j,z) a += (str[j] != ':' ? str[j] : '.');
	return a;
}

void Test::checkreal(const char * f, const char * s0, const char * s1, real v0, real v1) 
{
	bool fail = (fabs(v0 - v1) > sqrt(DBL_EPSILON)) || !finite(v0) || !finite(v1);
	printf("@%c %s: checkreal(%s=%g,%s=%g)\n",fail?'?':'!',f,s0,v0,s1,v1);
	_fail |= fail;
}

void Test::checkint(const char * f, const char * s0, const char * s1, int v0, int v1)
{
	bool fail = (v0 != v1);
	printf("@%c %s: checkint(%s=%d,%s=%d)\n",fail?'?':'!',f,s0,v0,s1,v1);
	_fail |= fail;
}

void Test::checkbool(const char * f, const char * s0, const char * s1, bool v0, bool v1) 
{
	bool fail = (v0 != v1);
	printf("@%c %s: checkbool(%s=%s,%s=%s)\n",fail?'?':'!',f,s0,v0?"true":"false",s1,v1?"true":"false");
	_fail |= fail;
}

void Test::checkstring(const char * f, const char * s0, const char * s1, const std::string & v0, const std::string & v1)
{
	bool fail = (v0 != v1);
	printf("@%c %s: checkstring(%s=\"%s\",%s=\"%s\")\n",fail?'?':'!',f,s0,escape(v0.data()).data(),s1,escape(v1.data()).data());
	_fail |= fail;
}

